package com.yxjx.duoxue.network;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.yxjx.duoxue.CipherHelper;
import com.yxjx.duoxue.EncryptGenAES;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.course.CourseOrderExtraInfo;
import com.yxjx.duoxue.course.CourseOrderKidsOption;
import com.yxjx.duoxue.course.datamodel.CouponListWrapper;
import com.yxjx.duoxue.dao.Dao;
import com.yxjx.duoxue.datamodel.DMActivity;
import com.yxjx.duoxue.datamodel.DMComment;
import com.yxjx.duoxue.datamodel.DMCommentLevelAvg;
import com.yxjx.duoxue.datamodel.DMCoupon;
import com.yxjx.duoxue.datamodel.DMKeyword;
import com.yxjx.duoxue.datamodel.DMObject;
import com.yxjx.duoxue.datamodel.DMOrder;
import com.yxjx.duoxue.datamodel.DMOrderListItem;
import com.yxjx.duoxue.datamodel.DMProduct;
import com.yxjx.duoxue.datamodel.DMTeacher;
import com.yxjx.duoxue.datamodel.ResponseObject;
import com.yxjx.duoxue.datamodel.course.DMCourse;
import com.yxjx.duoxue.datamodel.school.DMSchool;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.message.ClientMessageListDTS;
import com.yxjx.duoxue.network.NwUtils.*;
import com.yxjx.duoxue.payment.ClientUserKidsDTS;
import com.yxjx.duoxue.payment.ConfigKidsDetailDTS;
import com.yxjx.duoxue.payment.CouponListActivity;
import com.yxjx.duoxue.utils.FileHelper;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * request from server, which need user's login info.
 * @author mengmengchen
 *
 */
public class UserReg {
	
	// user reg and login.
	public static final String REQ_USER_MOBILE_CHECK = NwUtils.ROOT_URL + "/user/mobilecheck.json";
	public static final String REQ_USER_REG = NwUtils.ROOT_URL + "/user/reg.json";
	public static final String REQ_USER_REG_CODE = NwUtils.ROOT_URL + "/reg/mobilecode.json";
	public static final String REQ_USER_SALT = NwUtils.ROOT_URL + "/user/salt.json";
	public static final String REQ_USER_LOGIN = NwUtils.ROOT_URL + "/user/login.json";
	public static final String REQ_USER_VIEW = NwUtils.ROOT_URL + "/user/view.json";
	public static final String REQ_USER_EDIT = NwUtils.ROOT_URL + "/user/edit.json";
	public static final String REQ_MODIFY_PWD = NwUtils.ROOT_URL + "/user/changepwd.json";
	
	// favourite course.
	public static final String REQ_FAVOURITE_COURSE = NwUtils.ROOT_URL + "/favorite/favCourse.json";
	public static final String REQ_FAVOURITE_COURSE_LIST = NwUtils.ROOT_URL + "/favorite/listCourse.json";
	// reg activities.
	public static final String REQ_BOOK_ACTIVITY = NwUtils.ROOT_URL + "/appointment/bookActivity.json";
	public static final String REQ_BOOK_COURSE = NwUtils.ROOT_URL + "/appointment/bookCourse.json";
	public static final String REQ_BOOKED_ACTIVITES = NwUtils.ROOT_URL + "/appointment/listActivity.json";
	public static final String REQ_BOOKED_COURSES = NwUtils.ROOT_URL + "/appointment/listCourse.json";
	public static final String REQ_FORGET_PASSWORD = NwUtils.ROOT_URL + "/user/forget.json";
	// 点评相关.
	// file upload.
	public static final String REQ_FILE_UPLOAD = NwUtils.ROOT_URL + "/file/fileUpload.json";
	public static final String REQ_COMMENT_SCHOOL = NwUtils.ROOT_URL + "/comment/schoolComment.json";
	public static final String REQ_COMMENT_COURSE = NwUtils.ROOT_URL + "/comment/courseComment.json";
	public static final String REQ_LIST_USER_COMMENTS = NwUtils.ROOT_URL + "/comment/userCommentList.json";
	
	// 课程下单购买相关
	public static final String REQ_ORDER_COURSE = NwUtils.ROOT_URL + "/pay/courseOrder.json";//课程下单
	public static final String REQ_ORDER_LIST = NwUtils.ROOT_URL + "/pay/orderList.json";// 订单列表
	public static final String REQ_BACK_URL = NwUtils.ROOT_URL + "/back/backUrl.json"; // 回传url
	public static final String REQ_PRODUCT_COURSE = NwUtils.ROOT_URL + "/product/course.json"; // 商品详情
	public static final String REQ_REFUND_ORDER_SUBMIT = NwUtils.ROOT_URL + "/pay/refundOrder.json";
	public static final String REQ_PAY_SCHOOL_SUBMIT = NwUtils.ROOT_URL + "/pay/payOrder.json";
	// 优惠券(某个商品可用的优惠券列表)
	public static final String REQ_COUPON_LIST = NwUtils.ROOT_URL + "/user/coupon.json";
	// 消息中心和消息详情
	public static final String REQ_MESSAGE_LIST = NwUtils.ROOT_URL + "/message/list.json";
	public static final String REQ_MESSAGE_INFO = NwUtils.ROOT_URL + "/message/info.json";
	public static final String REQ_QUERY_NEW_MESSAGE = NwUtils.ROOT_URL + "/message/new.json";
	// 获取子女信息(下课程订单时填写)
	public static final String REQ_GET_USER_KID = NwUtils.ROOT_URL + "/user/getUserKids.json";
	// 当返回的code为－10时，从des字段读取错误描述信息。
	public static final String KEY_MESSAGE = "msg";
	
	// 保存学生信息
	public static final String REQ_SAVE_KID_INFO = NwUtils.ROOT_URL + "/user/setKids.json";
	// 我的账号获取用户信息
	public static final String REQ_GET_USER_INFO = NwUtils.ROOT_URL + "/user/userInfo.json";
	// 子女信息录入热门课程分类
	public static final String REQ_GET_KID_HOT_TYPES = NwUtils.ROOT_URL + "/config/kidsHotType.json";
	
	public static final int RSP_EXIST = 1;
	
	public static String getMobileCheckUrl(String phoneNum) {
		return REQ_USER_MOBILE_CHECK + "?mobile=" + phoneNum;
	}
	
	public static URMobileCheckResponse mobileCheck(String phoneNumber) throws ParseException, JSONException, IOException {
		String reqUrl = getMobileCheckUrl(phoneNumber);
		HttpPost request = new HttpPost(reqUrl);
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		
		URMobileCheckResponse rsp = new URMobileCheckResponse();
		
		rsp.setCode(jo.getInt("code"));
		if (rsp.getCode() == MsgCode.SUCCESS) {
			rsp.setExist(jo.getInt("exist"));
			if (rsp.getExist() != RSP_EXIST) {
				rsp.setSid(jo.getString("sid"));
				rsp.setSalt(jo.getString("salt"));
			}
		} else if (rsp.getCode() == MsgCode.ERROR_CODE_WITH_SPECIFIED_DES) {
			rsp.setMsg(DMObject.getString(jo, KEY_MESSAGE));
		}
		return rsp;
	}
	
	public static URMobileCheckResponse salt(String phoneNumber) throws ParseException, JSONException, IOException {
		String reqUrl = REQ_USER_SALT;
		HttpPost request = new HttpPost(reqUrl);
		
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("mobile", phoneNumber));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		
		URMobileCheckResponse rsp = new URMobileCheckResponse();
		rsp.setCode(jo.getInt("code"));
		if (rsp.getCode() == MsgCode.SUCCESS) {
			rsp.setSid(jo.getString("sid"));
			rsp.setSalt(jo.getString("salt"));
		} else if (rsp.getCode() == MsgCode.ERROR_CODE_WITH_SPECIFIED_DES) {
			rsp.setMsg(DMObject.getString(jo, KEY_MESSAGE));
		}
		return rsp;
	}
	
	/**
	 * 用户注册，保存当前用户的唯一ID。
	 * @param rsp
	 * @param userName
	 * @param pwd
	 * @param mobileCode
	 * @param ctx
	 * @return
	 * @throws JSONException
	 * @throws ParseException
	 * @throws IOException
	 */
	public static ResponseObject regUser(URMobileCheckResponse rsp, String userName, String pwd, String mobileCode, Context ctx) throws JSONException, ParseException, IOException {
		
		HttpPost request = new HttpPost(REQ_USER_REG);
		
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("mobile", userName));
		list.add(new BasicNameValuePair("password",getLongKey(rsp, pwd)));
		list.add(new BasicNameValuePair("sid", rsp.getSid()));
		if (!UIHelper.isEmpty(mobileCode)) {
			list.add(new BasicNameValuePair("checkCode",mobileCode));
		}
		String versionString = Utils.getFullDeviceId(ctx);
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(ctx)));
		list.add(new BasicNameValuePair("version", versionString));
		Utils.logd("version=" + versionString);
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		
		ResponseObject rspObj = new ResponseObject(jo.getInt("code"), "");
		if (rspObj.getCode() == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data");
			byte[] decryptedData = CipherHelper.decryptAES(
					encryptedData.getBytes(), getShortKey(rsp, pwd));
		} else if (rspObj.getCode() == MsgCode.ERROR_CODE_WITH_SPECIFIED_DES) {
			rspObj.setMsg(DMObject.getString(jo, KEY_MESSAGE));
		}
		
		return rspObj;
	}
	
	public static ResponseObject fetchPassword(URMobileCheckResponse rsp, String userName,
			String pwd, String mobileCode) throws JSONException,
			ParseException, IOException {

		HttpPost request = new HttpPost(REQ_FORGET_PASSWORD);
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("mobile", userName));
		list.add(new BasicNameValuePair("newPwd", pwd));
		list.add(new BasicNameValuePair("sid", rsp.getSid()));
		list.add(new BasicNameValuePair("checkCode", mobileCode));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));

		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity,
				"UTF-8"));

		ResponseObject rspRet = new ResponseObject(jo.getInt("code"), DMObject.getString(jo, KEY_MESSAGE));

		return rspRet;
	}
	
	public static UserLoginResponse userLogin(Context context, URMobileCheckResponse rsp, String phoneNum, String pwd) throws ParseException, JSONException, IOException {
		
		HttpPost request = new HttpPost(REQ_USER_LOGIN);
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("mobile", phoneNum));
		list.add(new BasicNameValuePair("sid", rsp.getSid()));
		JSONObject jo = new JSONObject();
		jo.put("time", System.currentTimeMillis());
		jo.put("password", getLongKey(rsp, pwd));
		jo.put("version", Utils.getSoftwareInfo());
		Log.d("CMM2", "data before = " + jo.toString());
		String data = EncryptGenAES.encrypt(jo.toString(), getShortKey(rsp, pwd));
		Log.d("CMM2", "data after = " + data);
		Log.d("CMM2", "key=" + getShortKey(rsp, pwd));
		list.add(new BasicNameValuePair("data",data));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		
		UserLoginResponse ulRsp = new UserLoginResponse();
		int code = jo.getInt("code");
		ulRsp.setCode(jo.getInt("code"));
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data");
			String decryptedData = EncryptGenAES.decrypt(encryptedData, getShortKey(rsp, pwd));
			JSONObject ret = new JSONObject(decryptedData);
			Log.d("CMM2", "decrypted=" + decryptedData);
			ulRsp.setuId(ret.getString("uid"));
			ulRsp.setSessionKey(ret.getString("sessionkey"));
			ulRsp.setSid(ret.getString("sid"));
			ulRsp.setPhoneNumber(phoneNum);
			ulRsp.setUserPassword(pwd);
		} else if (code == MsgCode.ERROR_CODE_WITH_SPECIFIED_DES) {
			ulRsp.setMsg(DMObject.getString(jo, KEY_MESSAGE));
		}
		
		if (code != MsgCode.SUCCESS) {
			return ulRsp;
		}
		
		ClientUserKidsDTS kidInfo = getUserInfo(context, ulRsp);
		if (kidInfo != null) {
			ulRsp.setKidInfo(kidInfo);
		}
		return ulRsp;
	}
	
	/**
	 * changepwd
	 * @param context
	 * @param oldPwd
	 * @param newPwd
	 * @param reNewPwd
	 * @return
	 */
	public static ResponseObject changepwd(Context context, String oldPwd, String newPwd, String reNewPwd) {
		UserLoginResponse ulRsp = Dao.getUlRsp(context);
		if (ulRsp == null) {
			return new ResponseObject(MsgCode.ERROR_CODE_WITH_SPECIFIED_DES, "请先重新登陆");
		}
		JSONObject params = new JSONObject();
		try {
			params.put("oldPwd", "" + oldPwd);
			params.put("newPwd", "" + newPwd);
			params.put("reNewPwd", "" + reNewPwd);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		JSONObject jo = null;
		try {
			jo = executeEncryptedRequestInternal(REQ_MODIFY_PWD, params, context, ulRsp);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		ResponseObject rspObj = new ResponseObject(DMObject.getInteger(jo, "code"), DMObject.getString(jo, KEY_MESSAGE));
		return rspObj;
	}
	
	/**
	 * request to book a activity or a course.
	 * @param ulRsp
	 * @param bookType
	 * @param bookInfo
	 * @return
	 * @throws JSONException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static ResponseObject requestBookItem(UserLoginResponse ulRsp, int bookType, JSONObject bookInfo, Context context) throws JSONException, ClientProtocolException, IOException {
		
		HttpPost request;
		if (bookType == Yx.TYPE_ITEM_ACTIVITY) {
			request = new HttpPost(REQ_BOOK_ACTIVITY);
		} else if (bookType == Yx.TYPE_ITEM_COURSE) {
			request = new HttpPost(REQ_BOOK_COURSE);
		} else {
			return new ResponseObject(-1, "");
		}
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		
		String data = EncryptGenAES.encrypt(bookInfo.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		ResponseObject rspObj = new ResponseObject(jo.getInt("code"), DMObject.getString(jo, KEY_MESSAGE));
		return rspObj;
	}
	
	/**
	 * request booked courses, activities or favorite courses.
	 * @param <E>
	 * @param ulRsp
	 * @param urmcRsp
	 * @param bookType
	 * @param params
	 * @return
	 * @throws JSONException 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static <E> ArrayList<E> requestBookedInfos(
			UserLoginResponse ulRsp, URMobileCheckResponse urmcRsp1,
			int bookType, JSONObject params,
			Context context) throws JSONException, ClientProtocolException, IOException {
		
		HttpPost request = null;
		if (bookType == Yx.TYPE_ITEM_ACTIVITY) {
			request = new HttpPost(REQ_BOOKED_ACTIVITES);
		} else if (bookType == Yx.TYPE_ITEM_COURSE) {
			request = new HttpPost(REQ_BOOKED_COURSES);
		} else {
			return null;
		}
		
		if (ulRsp == null) {
			// not login 
			return null;
		}
		
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		int code = jo.getInt("code");
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			if (bookType == Yx.TYPE_ITEM_COURSE) {
				ArrayList<DMCourse> ret = new ArrayList<DMCourse>();
				JSONArray jsonArray = retJson.getJSONArray("courses");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject item = jsonArray.getJSONObject(i);
					DMCourse course = new DMCourse();
					fillJsonToCourseData(item, course);
					ret.add(course);
				}
				return (ArrayList<E>) ret;

			} else if (bookType == Yx.TYPE_ITEM_ACTIVITY) {
				ArrayList<DMActivity> ret = new ArrayList<DMActivity>();
				JSONArray jsonArray = retJson.getJSONArray("courses");

				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject item = jsonArray.getJSONObject(i);
					DMActivity activity = new DMActivity();
					fillJsonToActivityData(item, activity);
					ret.add(activity);
				}
				return (ArrayList<E>) ret;
			}
		}
		return null;
	}

	private static void fillJsonToActivityData(JSONObject item,
			DMActivity activity) throws JSONException {
		activity.setActivityId(item.getLong("typeId"));
		activity.setActivityName(item.getString("actTitle"));
		activity.setAddress(item.getString("actAddress"));
		activity.setHeadImageUrl(item.getString("actImage"));
//		activity.setHostName(item.getString("schoolName"));
		activity.setSchoolGps(item.getString("gps"));
		activity.setActCount(item.getInt("actCount"));
		activity.setActivityDes(item.getString("actDes"));
		activity.setActNowCount(item.getInt("actNowCount"));
		activity.setContactPhone(item.getString("contactPhone"));
		activity.setEndTime(item.get("actEndTime"));
		activity.setStartTime(item.get("actBeginTime"));
	}

	private static void fillJsonToCourseData(JSONObject item, DMCourse course)
			throws JSONException {
		course.setCourseId(item.getLong("typeId"));
		course.setCourseName(item.getString("courseName"));
		course.setCourseType1(item.getString("firstTypeName"));
		course.setCourseType2(item.getString("secondTypeName"));
		course.setPropAgeStart(NwUtils.getInt(item, "propAgeStart", 0));
		course.setPropAgeEnd(NwUtils.getInt(item, "propAgeEnd", 0));
		course.setSchoolFullName(item.getString("schoolFullName"));
		course.setHeadImageUrl(item.getString("courseImage"));
		course.setSchoolGps(item.getString("gps"));
		course.setSchoolId(item.getLong("schoolId"));
		course.setAddress(item.getString("address"));

	}
	
	public static ResponseObject requestFavouriteItem(UserLoginResponse ulRsp, int favouriteType, JSONObject favouriteInfo,
			Context context) throws JSONException, ClientProtocolException, IOException {
		
		HttpPost request;
		if (favouriteType == Yx.TYPE_ITEM_COURSE) {
			request = new HttpPost(REQ_FAVOURITE_COURSE);
		} else {
			return new ResponseObject(-1, "");
		}
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		
		String data = EncryptGenAES.encrypt(favouriteInfo.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		UserLoginResponse ULrsp = new UserLoginResponse();
		return new ResponseObject(jo.getInt("code"), DMObject.getString(jo, KEY_MESSAGE));
	}
	
	/**
	 * return all favourite courses.
	 * @param ulRsp
	 * @param favType
	 * @param params
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * @throws JSONException 
	 * @throws ParseException 
	 */
	public static ArrayList<DMCourse> requestFavouriteCourses(
			UserLoginResponse ulRsp, int favType, JSONObject params,
			Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		
		HttpPost request = null;
		if (favType == Yx.TYPE_ITEM_COURSE) {
			request = new HttpPost(REQ_FAVOURITE_COURSE_LIST);
		} else {
			return null;
		}
		
		if (ulRsp == null) {
			// not login 
			return null;
		}
		
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		UserLoginResponse ULrsp = new UserLoginResponse();
		ArrayList<DMCourse> ret = new ArrayList<DMCourse>();
		int code = jo.getInt("code");
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			JSONArray jsonArray = retJson.getJSONArray("favorites");
			int jsonLen = jsonArray.length();
			for (int i = 0; i < jsonLen; i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				DMCourse course = new DMCourse();
				
				course.setCourseId(item.getLong("typeId"));
				course.setHeadImageUrl(item.getString("courseImage"));
				course.setCourseName(item.getString("courseName"));
				course.setCourseType1(item.getString("firstTypeName"));
				course.setCourseType2(item.getString("secondTypeName"));
				course.setPropAgeStart(NwUtils.getInt(item, "propAgeStart", 0));
				course.setPropAgeEnd(NwUtils.getInt(item, "propAgeEnd", 0));
				course.setSchoolFullName(item.getString("schoolFullName"));
				course.setSchoolId(item.getLong("schoolId"));
				course.setAddress(item.getString("address"));
				course.setSchoolGps(item.getString("gps"));
				ret.add(course);
			}
			return ret;
		}
		return ret;
		
	}
	
	/**
	 * return my order list.
	 * @param ulRsp
	 * @param params
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * @throws JSONException 
	 * @throws ParseException 
	 */
	public static ArrayList<DMOrderListItem> requestOrderList(
			UserLoginResponse ulRsp, JSONObject params,
			Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		
		HttpPost request = new HttpPost(NwUtils.REQ_ORDER_LIST);
		
		List list = new ArrayList<BasicNameValuePair>();
		if (ulRsp != null) {
			list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		}
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		UserLoginResponse ULrsp = new UserLoginResponse();
		ArrayList<DMOrderListItem> ret = new ArrayList<DMOrderListItem>();
		int code = jo.getInt("code");
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			JSONArray jsonArray = retJson.getJSONArray("dtsList");
			int jsonLen = jsonArray.length();
			for (int i = 0; i < jsonLen; i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				DMOrderListItem order = DMOrderListItem.from(item.toString());
				ret.add(order);
			}
			return ret;
		}
		return ret;
	}
	
	/**
	 * request one Order info
	 * @param ulRsp
	 * @param params
	 * @param context
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws ParseException
	 * @throws JSONException
	 */
	public static DMOrder requestOrderInfo(
			UserLoginResponse ulRsp, JSONObject params,
			Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		
		HttpPost request = new HttpPost(NwUtils.REQ_ORDER_INFO);
		
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		UserLoginResponse ULrsp = new UserLoginResponse();
		int code = jo.getInt("code");
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject jo1 = new JSONObject(decryptedStr);
			DMOrder ret = DMOrder.from(jo1.getString("dts"));
			return ret;
		}
		return null;
	}
	
	private static String getShortKey(URMobileCheckResponse rsp, String pwd) {
		String sha1 = CipherHelper.SHA1(pwd + rsp.getSalt());
		return sha1.substring(0, 16);
	}
	
	private static String getLongKey(URMobileCheckResponse rsp, String pwd) {
		return CipherHelper.SHA1(pwd + rsp.getSalt());
	}
	
	/**
	 * 上传文件单个文件到服务器
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * @throws JSONException 
	 * @throws ParseException 
	 */
	public static String uploadFile(UserLoginResponse ulRsp, String filePath, Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		HttpPost request = new HttpPost(REQ_FILE_UPLOAD);
		if (ulRsp == null) {
			// not login 
			return "";
		}
		// check file exist or not
		File uploadFile = new File(filePath);
		if (!uploadFile.exists()) {
			return null;
		}
		
        MultipartEntity reqEntity = new MultipartEntity();
        // add sid
        reqEntity.addPart("sid", new StringBody(ulRsp.getSid()));
        // add encrypted data.
        JSONObject params = new JSONObject();
        params.put("md5", FileHelper.getFileMD5(uploadFile));
        params.put("fname", filePath);
        params.put("time", "" + System.currentTimeMillis());
        params.put("size", "" + FileHelper.getFileSize(uploadFile));
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		reqEntity.addPart("data", new StringBody(data));
		// add file
		FileBody fileBody = new FileBody(uploadFile);
        reqEntity.addPart("file", fileBody); 
        reqEntity.addPart("device", new StringBody(Utils.getShortDeviceId(context)));
		
		request.setEntity(reqEntity);
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		int code = jo.getInt("code");
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			String url = retJson.getString("url");
			return url;
		}
		return "";
	}
	
	public static ResponseObject submitComment(UserLoginResponse ulRsp, int commentType, JSONObject commentObject,
			Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		HttpPost request;
		if (commentType == Yx.TYPE_ITEM_COURSE) {
			request = new HttpPost(REQ_COMMENT_COURSE);
		} else if (commentType == Yx.TYPE_ITEM_SCHOOL) {
			request = new HttpPost(REQ_COMMENT_SCHOOL);
		} else {
			return new ResponseObject(-1, "");
		}
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		
		String data = EncryptGenAES.encrypt(commentObject.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		return new ResponseObject(jo.getInt("code"), DMObject.getString(jo, KEY_MESSAGE));
	}
	
	/**
	 * 读取当前用户的点评历史
	 * @param ulRsp
	 * @param params
	 * @return
	 * @throws IOException 
	 * @throws JSONException 
	 * @throws ParseException 
	 */
	public static ArrayList<DMComment> getMyComments(UserLoginResponse ulRsp, JSONObject params,
			Context context) throws ParseException, JSONException, IOException {
		ArrayList<DMComment> ret = null;
		HttpPost request = new HttpPost(REQ_LIST_USER_COMMENTS);

		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		int code = jo.getInt("code");
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			JSONArray jsonCommentsArray = retJson.getJSONArray("comments");
			ret = new ArrayList<DMComment>();
			NwUtils.fillDataToCommentArray(retJson, ret);
		}
		return ret;
	}
	
	/**
	 * 提交课程订单
	 * @param ulRsp
	 * @param product
	 * @param selectedCoupon
	 * @param contactName
	 * @param contactPhone
	 * @param parentSex
	 * @param extraInfo
	 * @return
	 * @throws JSONException 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static DMOrder requestCourseOrder(Context context,
			UserLoginResponse ulRsp, DMProduct product,
			DMCoupon selectedCoupon, String contactName, String contactPhone,
			int parentSex, CourseOrderExtraInfo extraInfo) throws JSONException,
			ClientProtocolException, IOException {
		
		DMOrder order = null;
		HttpPost request = new HttpPost(REQ_ORDER_COURSE);
		JSONObject params = new JSONObject();
		params.put("courseId", product.getProductTypeId());
		params.put("productId", product.getId());
		//
		// 使用优惠券selectedCoupon
		if (selectedCoupon != null) {
			params.put("couponIdString", "" + selectedCoupon.getId());
		}
		//
		params.put("contactName", contactName);
		params.put("contactPhone", contactPhone);
		params.put("contactSex", "" + parentSex);
		
		// 是否提交额外信息
		if (extraInfo != null) {
			params.put("isShow", "" + extraInfo.getIsShow());
			params.put("kidsName", "" + extraInfo.getKidsName());
			params.put("kidsAge", "" + extraInfo.getKidsAge());
			params.put("kidsSex", "" + extraInfo.getKidsSex());
			params.put("kidsDetailIdString", "" + extraInfo.getKidsDetailIdString());
			params.put("studentBirthYear", "" + extraInfo.getStudentBirthYear());
			params.put("studentBirthMonth", "" + extraInfo.getStudentBirthMonth());
		}
		params.put("time", System.currentTimeMillis());
		
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));

		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		order = new DMOrder();
		int code = jo.getInt("code");
		order.setOrderRspCode(new ResponseObject(code, DMObject.getString(jo, KEY_MESSAGE)));
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			String orderId = (String) retJson.get("orderId");
			order.setOrderId(orderId);
			order.setContactName(contactName);
			order.setContactPhone(contactPhone);
			order.setProduct(product);
		}
		return order;
	}
	
	/**
	 * 请求回退报名费
	 * @param ulRsp
	 * @param params
	 * @param context
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws ParseException
	 * @throws JSONException
	 */
	public static ResponseObject requestRefundOrder(
			UserLoginResponse ulRsp, JSONObject params,
			Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		
		HttpPost request = new HttpPost(REQ_REFUND_ORDER_SUBMIT);
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		return new ResponseObject(jo.getInt("code"), DMObject.getString(jo, KEY_MESSAGE));
	}
	
	/**
	 * 请求报名费交付给机构
	 * @param ulRsp
	 * @param params
	 * @param context
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws ParseException
	 * @throws JSONException
	 */
	public static ResponseObject requestPaySchool(
			UserLoginResponse ulRsp, JSONObject params,
			Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		
		HttpPost request = new HttpPost(REQ_PAY_SCHOOL_SUBMIT);
		List list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		return new ResponseObject(jo.getInt("code"), DMObject.getString(jo, KEY_MESSAGE));
	}
	
	/**
	 * return my coupon list.
	 * @param params
	 * @param context
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * @throws JSONException 
	 * @throws ParseException 
	 */
	public static CouponListWrapper requestCouponList(
			JSONObject params,
			Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		
		HttpPost request = new HttpPost(REQ_COUPON_LIST);
		
		List list = new ArrayList<BasicNameValuePair>();
		// TODO
		UserLoginResponse ulRsp = UserLoginResponse.getInstance(context);
		if (ulRsp != null) {
			list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		}
		
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data",data));
		list.add(new BasicNameValuePair("device", Utils.getShortDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		UserLoginResponse ULrsp = new UserLoginResponse();
		CouponListWrapper ret = new CouponListWrapper();
		int code = jo.getInt("code");
		if (code == MsgCode.SUCCESS) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			JSONArray jsonArray = retJson.getJSONArray("couponList");
			int jsonLen = jsonArray.length();
			for (int i = 0; i < jsonLen; i++) {
				JSONObject item = jsonArray.getJSONObject(i);
				DMCoupon order = DMCoupon.from(item.toString());
				if (order != null) {
					ret.add(order);
				}
			}
			return ret;
		}
		return null;
	}
	
	/**
	 * 请求消息中心的消息列表
	 * @param params
	 * @param context
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws ParseException
	 * @throws JSONException
	 */
	public static ArrayList<ClientMessageListDTS> requestMessageList(JSONObject params, Context context) throws ClientProtocolException, IOException, ParseException, JSONException {
		JSONObject jo = executeRequestInternal(REQ_MESSAGE_LIST, params, context);
		int code = jo.getInt("code");
		if (code == MsgCode.SUCCESS) {
			JSONArray ja = jo.getJSONArray("messageList");
			ArrayList<ClientMessageListDTS> array = new ArrayList<ClientMessageListDTS>();
			for (int i = 0; i < ja.length(); i++) {
				JSONObject joItem = ja.getJSONObject(i);
				ClientMessageListDTS item = ClientMessageListDTS.from(joItem.toString());
				if (item != null) {
					array.add(item);
				}
			}
			return array;
		}
		return null;
	}

	private static JSONObject executeRequestInternal(String requestString, JSONObject params,
			Context context) throws UnsupportedEncodingException, IOException,
			ClientProtocolException, JSONException {
		HttpPost request = new HttpPost(requestString);
		List list = new ArrayList<BasicNameValuePair>();
		UserLoginResponse ulRsp = UserLoginResponse.getInstance(context);
		if (ulRsp != null) {
			list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		}
		list.add(new BasicNameValuePair("param", "" + params.toString()));
		list.add(new BasicNameValuePair("device", Utils.getFullDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		return jo;
	}
	
	private static JSONObject executeEncryptedRequestInternal(String requestString, JSONObject params,
			Context context, UserLoginResponse ulRsp) throws UnsupportedEncodingException, IOException,
			ClientProtocolException, JSONException {
		HttpPost request = new HttpPost(requestString);
		List list = new ArrayList<BasicNameValuePair>();
		if (ulRsp != null) {
			list.add(new BasicNameValuePair("sid", ulRsp.getSid()));
		}
		String data = EncryptGenAES.encrypt(params.toString(), ulRsp.getSessionKey());
		list.add(new BasicNameValuePair("data", data));
		list.add(new BasicNameValuePair("device", Utils.getFullDeviceId(context)));
		request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
		HttpResponse response = new DefaultHttpClient().execute(request);
		HttpEntity responseEntity = response.getEntity();
		JSONObject jo = new JSONObject(EntityUtils.toString(responseEntity, "UTF-8"));
		return jo;
	}
	
	/**
	 * 请求该条message，这样后台会将该条消息的状态置为已读。
	 * @param params
	 * @param context
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 */
	public static boolean requestMessageInfo(JSONObject params, Context context) throws UnsupportedEncodingException, ClientProtocolException, IOException, JSONException {
		JSONObject jo = executeRequestInternal(REQ_MESSAGE_INFO, params, context);
		return jo.getInt("code") == MsgCode.SUCCESS;
	}
	
	/**
	 * 检查是否有新的未读消息
	 * @param params
	 * @param context
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 */
	public static boolean checkNewMessageAvaiable(JSONObject params, Context context) throws UnsupportedEncodingException, ClientProtocolException, IOException, JSONException {
		JSONObject jo = executeRequestInternal(REQ_QUERY_NEW_MESSAGE, params, context);
		int code = jo.getInt("code");
		
		if (code == MsgCode.SUCCESS) {
			return DMObject.castInteger(jo,"newMsg") == 1;
		}
		return false;
	}
	
	/**
	 * 获取当前用户的kids消息
	 * @param params
	 * @param context
	 * @param force
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 */
	public static boolean getUserKids(JSONObject params, Context context, boolean force) throws UnsupportedEncodingException, ClientProtocolException, IOException, JSONException {
		
		Utils.logd("getUserKids 1");
		// 如果kidOption不为空并且非强制获取，则直接返回。
		if (Globals.getInstance(context).getKidOption() != null 
				&& Globals.getInstance(context).getKidOption().getKidsConfigList() != null
				&& Globals.getInstance(context).getKidOption().getKidsConfigList().size() > 0
				&& !force) {
			return false;
		}
		//
		UserLoginResponse ulRsp = UserLoginResponse.getInstance(context);
		Utils.logd("getUserKids 2 ulRsp = " + ulRsp);

		if (ulRsp == null) {
			return false;
		}
		JSONObject jo = executeEncryptedRequestInternal(REQ_GET_USER_KID, params, context, ulRsp);
		int code = jo.getInt("code");
		// TODO: update kids info to appDataModel.
		Utils.logd("getUserKids 3 code = " + code);

		if (code != MsgCode.SUCCESS) {
			return false;
		} else {
			Globals appData = Globals.getInstance(context);
			appData.setKidOption(null);
			
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			
			Utils.logd("getUserKids 3.5 retJson = " + retJson);

			appData.setKidOption(new CourseOrderKidsOption());
			appData.getKidOption().setShowCode(DMObject.castInteger(retJson,"isShowCode") == 1);
			appData.getKidOption().setHasKids(DMObject.castInteger(retJson,"hasKids"));
			appData.getKidOption().setAllowSkip(DMObject.getInteger(retJson, "allowSkip"));
			// 更新
			JSONArray ja = retJson.getJSONArray("kidsConfigList");
			appData.getKidOption().setKidsConfigList(new ArrayList<ConfigKidsDetailDTS>());
			for (int i = 0; i < ja.length(); i++) {
				ConfigKidsDetailDTS item = ConfigKidsDetailDTS.from(ja.getJSONObject(i).toString());
				if (item != null) {
					appData.getKidOption().getKidsConfigList().add(item);
				}
			}
			// server return may have no this key

			JSONObject kidsJson = DMObject.getJSONObject(retJson,"kids");
			Utils.logd("getUserKids 4 kidsJson = " + kidsJson);
			if (kidsJson != null) {
				ClientUserKidsDTS kidInfo = ClientUserKidsDTS.from(kidsJson.toString());
				Dao.setUserKidInfo(context, kidInfo);
			}
			return true;
		}
		
	}
	
	
	/**
	 * 保存学生信息
	 * @param params
	 * @param context
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 */
	public static ResponseObject saveKidInfo(JSONObject params, Context context) throws UnsupportedEncodingException, ClientProtocolException, IOException, JSONException {
		UserLoginResponse ulRsp = UserLoginResponse.getInstance(context);
		JSONObject jo = executeEncryptedRequestInternal(REQ_SAVE_KID_INFO, params, context, ulRsp);
		ResponseObject rspObj = new ResponseObject(jo.getInt("code"), "");
		if (rspObj.getCode() == MsgCode.ERROR_CODE_WITH_SPECIFIED_DES) {
			rspObj.setMsg(DMObject.getString(jo, KEY_MESSAGE));
		}
		return rspObj;
	}
	
	/**
	 * 获取学生信息, 在获取订单时也会获取学信息。
	 * 登录与获取学生信息是一致的，所以保存在登录信息对象里，将学生信息也同步更新道shared preference，
	 * 此外，所有获取修改学生信息时，都要同步到shared preference。
	 * @param context
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 */
	private static ClientUserKidsDTS getUserInfo(Context context, UserLoginResponse ulRsp) throws UnsupportedEncodingException, ClientProtocolException, IOException, JSONException {
		JSONObject params = new JSONObject();
		params.put("time", "" + System.currentTimeMillis());
		
		ClientUserKidsDTS kidInfo = null;
		JSONObject jo = executeEncryptedRequestInternal(REQ_GET_USER_INFO, params, context, ulRsp);
		int code = DMObject.castInteger(jo, "code");
		if (code == MsgCode.SUCCESS && ulRsp != null) {
			String encryptedData = jo.getString("data"); 
			String decryptedStr = EncryptGenAES.decrypt(encryptedData, ulRsp.getSessionKey());
			JSONObject retJson = new JSONObject(decryptedStr);
			
			if (DMObject.getInteger(retJson, "code") == MsgCode.SUCCESS) {
				JSONObject kidJson = DMObject.getJSONObject(retJson,"kids");
				if (kidJson != null) {
					kidInfo = ClientUserKidsDTS.from(kidJson.toString());
				}
			}
		}
		return kidInfo;
	}
	
}
